package com.hailian.fintech.hlhouse_statusserver.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hailian.fintech.hlhouse_statusserver.entity.Area;
import com.hailian.fintech.hlhouse_statusserver.mapper.AreaMapper;
import com.hailian.fintech.hlhouse_statusserver.service.IAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hanbo
 * @since 2020-06-01
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements IAreaService{

    //注入areamapper
    @Autowired
    private AreaMapper areaMapper;


    /**
     * 实现三级联动
     * @return
     */
    @Override
    public List<Area> getAreaList() {
        //查询出area表的所有数据
        List<Area> all = areaMapper.findAll();
        //根据pid==1查询出所有的省
        List<Area> provinceList = areaMapper.findByPid(1);
        //具体实现
        provinceList.forEach(province->{
            List<Area> citylist = new ArrayList<>();
            all.forEach(city->{
                if (city.getPid().intValue()==province.getId().intValue()){
                    citylist.add(city);
                    List<Area> areaList = new ArrayList<>();
                    all.forEach(area -> {
                        if (area.getPid().intValue()==city.getId().intValue()){
                            areaList.add(area);
                        }
                    });
                    city.setSubAreaList(areaList);
                }
            });
            province.setSubAreaList(citylist);
        });
        return provinceList;
    }
}
